#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/4/24 14:53
# @Author  : Grayson Liu
# @Email   : graysonliu@foxmail.com
# @File    : CustomSimpleMethodListWidget.py
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QHeaderView, QAbstractItemView, QTableWidgetItem

from ui.Ui_CustomListWidget import Ui_CustomListWidget
from CustomSignal import CustomSignal


class CustomSimpleMethodListWidget(QWidget, Ui_CustomListWidget):
    """
    自定义的表格控件，属于QWidget，继承自Ui_CustomListWidget，用于显示方法列表(没有操作栏按钮，仅显示方法和参数)
    """
    global_signal = CustomSignal()

    def __init__(self, parent=None):
        """
        初始化
        :param parent: 父窗体
        """
        super(CustomSimpleMethodListWidget, self).__init__(parent)
        self.setupUi(self)

        # 初始化tableWidget
        self.tableWidget.setColumnCount(2)
        self.tableWidget.setHorizontalHeaderLabels(["方法", "参数"])  # 表头
        self.tableWidget.horizontalHeader().setFont(QFont("宋体", 8, QFont.Black))  # 表头的字体
        self.tableWidget.verticalHeader().setFont(QFont("Arial", 8))  # 列序号的字体
        # 设置表头的宽度
        self.tableWidget.setColumnWidth(0, 100)
        self.tableWidget.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
        self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)  # 不可编辑
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)  # 仅选择行

    def add_list_item(self, method_name: str, parameters: str):
        """
        对CustomSimpleMethodListWidget控件，根据传入的信息，在表格中添加一行
        :param method_name:
        :param parameters:
        :return:
        """
        item_name = QTableWidgetItem(method_name)
        item_name.setFont(QFont("宋体", 8))
        item_parameters = QTableWidgetItem(parameters)
        item_parameters.setFont(QFont("Arial", 8))
        row = self.tableWidget.rowCount()
        self.tableWidget.insertRow(row)
        self.tableWidget.setItem(row, 0, item_name)
        self.tableWidget.setItem(row, 1, item_parameters)

        self.tableWidget.setRowHeight(row, 25)

    def clear_list_item(self):
        """
        清空CustomMethodListWidget控件的所有内容
        :return:
        """
        self.tableWidget.clearContents()
        self.tableWidget.setRowCount(0)
